/**
 * 游戏界面
 * @author none
 */
class ZJHGameModule extends base.BaseGameSceneModule {
    public static request: any = {
        onStartEffectComplete: "onStartEffectComplete",
        onLookCard: "lookCard",
        onSettingButton: "onSettingButton",
        onBackButton: "onBackButton"
    }
    public static response: any = {
        onLunci: CMDConfig.GET_POOLTOPLAYER,
        onAutoLead: "autoLead",//倒计时结束自动出牌
        onGetLookCard: CMDConfig.GET_LOOKCARDS,//收到看牌
        onCompareCard: CMDConfig.GET_COMPARECARD//收到比牌
    }

    public dailog: how.Dialog;
    public alert: how.Alert;

    protected onGUIComplete(event: egret.Event): void 
    {
         console.log(">>> ZJHGameModule 初始化所有PlayerModule");
        //初始化所有PlayerModule
        for (var i = 0; i < 5; i++) {
            this["player" + i] = this.moduleManager.initModule(ZJHPlayerModule, this.gui["player" + i], ZJHPlayerData);
        }
        this.moduleManager.initModule(ZJHOperatePanelModule, this.gui["operatePanel"]);
        //初始化玩家数据
        this.initPlayers();
        //同步牌桌信息
        if (AppData.getInstance().userData.status != UserStatus.PLAY) 
        {
            this.sendSitDown();
        } 
        else 
        {
            this.initPlayers();
        }
        //同步牌桌信息
        this.hallSocket.send(CMDConfig.SEND_SYNTABLE, { bAllowLookon: 1 });
        super.onGUIComplete(event);
        how.SoundManager.playMusic("zjh_music_game_mp3");
    }
    //加载玩家数据
    public initPlayers(): void 
    {
        for (var j = 0; j < ZJHCardLogic.MAX_PLAYER; j++) 
        {
            if (!this["player" + j].data) 
            {
                egret.Tween.removeTweens(this["player" + j]);
                this["player" + j].data = null;
                this["player" + j].openCardData = null;
            }
        }
        super.initPlayers();
    }
    //发送坐下消息
    public sendSitDown() {
        this.hallSocket.send(CMDConfig.SEND_SIT, {
            wTableID: 0,
            wChairID: 65535,
            szTablePass: AppData.getInstance().currentRoom.szTablePass,
            cbPassLen: AppData.getInstance().currentRoom.cbPassLen,
            CellScore: AppData.getInstance().currentRoom.cellScore,
            PreScore: AppData.getInstance().currentRoom.preScore
        });
    }
    //收到游戏开始
    public onGameStart(data: any): void 
    {
        console.log(">>> ZJHGameModule onGameStart收到游戏开始");
        this.callData("onGameStart",data);
        var gameData: ZJHGameData = <ZJHGameData>AppData.getInstance().gameData;
        gameData.isLooked = false;
        gameData.operateData = data;
        gameData.currentScore = gameData.lCellScore;
        gameData.currentScoreCount = gameData.getPlayersByTableIDHaveChairID().length * gameData.currentScore;
        this.callUI("onGameStart", AppData.getInstance().gameData);
        super.onGameStart(data);
    }
    //收到游戏结束
    public onGameEnd(data: any): void 
    {
        console.log(">>> ZJHGameModule onGameEnd 收到游戏结束");
        this.callUI("onGameEnd", AppData.getInstance().gameData);
        super.onGameEnd(data);
    }
    //收到轮次变化
    public onLunci(data: any): void 
    {
        console.log(">>> ZJHGameModule onLunci 收到轮次变化");
        this.gameData.roundNum = data.roundNum;
        this.callUI("setLunciUI", data);
    }
    //收到操作结果
    public onValidateOperate(data: any) 
    {
        //"wCurrentUser":0,"wAddScoreUser":4,"wCompareState":0,"lAddScoreCount":50,"lCurrentTimes":5
        console.log(">>> ZJHGameModule onValidateOperate 收到操作结果");
        var gdata: ZJHGameData = <ZJHGameData>AppData.getInstance().gameData;
        gdata.operateData = data;
        gdata.addState = gdata.lCurrentTimes==data.lCurrentTimes?0:1;
        gdata.lCurrentTimes = data.lCurrentTimes;
        gdata.currentScore =gdata.lCurrentTimes * gdata.lCellScore * (gdata.isLooked ? 2 : 1);
        gdata.currentScoreCount += isNaN(gdata.operateData.lAddScoreCount)?0:gdata.operateData.lAddScoreCount;
        super.onValidateOperate(data);
        this.callUI("setOperateUI", gdata);
    }
    public onStartEffectComplete(): void {
        this.communicate(ZJHPlayerModule.response.setOperateUI, (<ZJHGameData>AppData.getInstance().gameData).operateData);
    }
    /*
     * 倒计时结束自动出牌
     */
    public onAutoLead(): void 
    {
        console.log(">>> ZJHGameModule onAutoLead 倒计时结束自动出牌");
        //停止自己出牌倒计时
        this.communicate(ZJHPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
        this.hallSocket.send(CMDConfig.SEND_QIPAi);
    }
    /**
     * 看牌
     */
    public onLookCard(): void 
    {
        console.log(">>> ZJHGameModule onLookCard 看牌");
        this.hallSocket.send(CMDConfig.SEND_LOOKCARDS, {});
    }
     /**
     * 设置
     */
    public onSettingButton(): void 
    {
        how.SettingWindow.show();
    }
    /**
     * 返回
     */
    public onBackButton(): void 
    {
        if (this.checkPlayerisGaming()) {
            this.alert = how.Alert.show(LanguageConfig.onBackButtonPlayerisGaming, () => {
                this.alert = null;
            });
        } else {
            this.dailog = how.Dialog.show(LanguageConfig.onBackButtonTip, this.sendBack, function () {
                this.dailog = null;
            }, this);
        }
    }
    /*
    * 发送返回消息
    * */
    public sendBack(): void 
    {
        this.hallSocket.send(CMDConfig.SEND_LEAVE_GAME, {});
        this.hallSocket.send(CMDConfig.SEND_LEAVE_ROOM, {});
    }
    /*
    * 判断玩家是否在游戏中
    * */
    private checkPlayerisGaming(): Boolean 
    {
        console.log(">>> ZJHGameModule checkPlayerisGaming 判断玩家是否在游戏中");
        var playerData = base.Utils.getItem(ZJHGameData.getInstance().playerList, "id", AppData.getInstance().userData.id);
        if (playerData && playerData.status == UserStatus.PLAY) 
        {
            return true;
        } 
        else 
        {
            return false;
        }
    }
    /**
     * 收到看牌消息
     */
    public onGetLookCard(data: any): void 
    {
        console.log(">>> ZJHGameModule onGetLookCard 收到看牌消息");
        if (data.wLookCardUser == AppData.getInstance().userData.chairID) {//如果是自己看的牌
            this.gameData.isLooked = true;
            this.gameData.currentScore = this.gameData.currentScore * 2;
            this.callUI("setCardUI", data.cbCardData);
            how.SoundManager.playEffect("zjh_opencard_mp3");
        }
    }
    /**
     * 收到比牌消息
     */
    public onCompareCard(data: any): void 
    {
        console.log(">>> ZJHGameModule onCompareCard 收到比牌消息");
        this.callUI("setCompareUI", data);
    }
    
    private get gameData(): ZJHGameData 
    {
        return <ZJHGameData>AppData.getInstance().gameData;
    }
    
}